package com.biostime.qdingding.modules.qd.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Activity;
import com.biostime.qdingding.modules.qd.entity.Activitymember;
import com.biostime.qdingding.modules.qd.entity.Member;
import com.biostime.qdingding.modules.qd.service.ActivityService;
import com.biostime.qdingding.modules.qd.service.ActivitymemberService;
import com.biostime.qdingding.modules.qd.service.MemberService;

/**
 * 会员活动Controller
 * @author king
 * @version 2015-12-15
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/activitymember")
public class ActivitymemberController extends BaseController {

	@Autowired
	private ActivitymemberService activitymemberService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private MemberService memberService;
	
	@ModelAttribute
	public Activitymember get(@RequestParam(required=false) String id) {
		Activitymember entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = activitymemberService.get(id);
		}
		if (entity == null){
			entity = new Activitymember();
		}
		return entity;
	}
	
	@RequestMapping("verify")
	public String verify(Activitymember activitymember,HttpServletRequest request,HttpServletResponse response, Model model){
		activitymember.setState(request.getParameter("verify"));
		activitymemberService.save(activitymember);
		return enroll(new Activitymember(),request,response,model);
	}
	
	@RequiresPermissions("qd:activitymember:view")
	@RequestMapping(value = {"enroll"})
	public String enroll(Activitymember activitymember, HttpServletRequest request, HttpServletResponse response, Model model) {
		activitymember.setType("enroll");
		Page<Activitymember> page = activitymemberService.findPage(new Page<Activitymember>(request, response), activitymember); 
		model.addAttribute("page", page);
		return "modules/qd/activitymemberList";
	}
	
	@RequiresPermissions("qd:activitymember:view")
	@RequestMapping(value = {"collect"})
	public String collect(Activitymember activitymember, HttpServletRequest request, HttpServletResponse response, Model model) {
		activitymember.setType("collect");
		Page<Activitymember> page = activitymemberService.findPage(new Page<Activitymember>(request, response), activitymember); 
		model.addAttribute("page", page);
		return "modules/qd/activitymemberList";
	}

	@RequiresPermissions("qd:activitymember:view")
	@RequestMapping(value = "form")
	public String form(Activitymember activitymember, Model model) {
		
		List<Activity> activitiyList = activityService.findList(new Activity());
		
		List<Member> memberList = memberService.findList(new Member());
		
		model.addAttribute("activitiyList", activitiyList);
		model.addAttribute("memberList", memberList);
		model.addAttribute("activitymember", activitymember);
		return "modules/qd/activitymemberForm";
	}

	@RequiresPermissions(value={"qd:activitymember:edit","qd:activitymember:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Activitymember activitymember, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, activitymember)){
			return form(activitymember, model);
		}
		activitymemberService.save(activitymember);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/activitymember/?repage";
	}
	
	@RequiresPermissions("qd:activitymember:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Activitymember activitymember, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				activitymemberService.delete(new Activitymember(id));
			}
		}else{
			activitymemberService.delete(activitymember);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/activitymember/?repage";
	}

}